var app = getApp(), fetchApi = require("../../../../utils/httpPost.js"),
commonJs = require("../../../../component/common.js");
import Dialog from '../../../../component/vant/dialog/dialog';
import Toast from '../../../../component/vant/toast/toast';
const message = '是否确认取消该订单？'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    imgurl: app.getImgUrl(),
    states1: [0, 1, 2],
    currentTab: 0,
    orderType: 3,
    orderstate:-1,
    pageSize:5,
    pageIndex: 1,
    scrollTop: 0,
    scrollHeight:0,
    hasData:false,
    orderList:[],
    loading: false,
    netWork: false,
    isLoading: true,
    visible:false,
    itemData:[],
    from_type:0,
    searchValue:'',
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('productList options',options)
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var isLogin = app.getStorageSync("isLogin");
    if (!isLogin) {
      wx.navigateTo({
        url: '/pages/login/index',
      })
    }else{
      this.initOp();
    }
  },
  initOp:function(){
    this.data.pageIndex = 1
    this.setData({
      loading: false,
      netWork: false,
      isLoading: true,
      hasData:false,
      orderList:[]
    })
    this.onHttpInit()
  },
  /**
   * 获取页面数据的 http请求
   */
  onHttpInit: function () {
    var that = this;
    fetchApi.httpPost('/device/water/order/list', {
      // state: state,
      order_type: that.data.orderType, //订单类型  0 - 水卡打水订单  1 - 扫码打水订单  2 - 水卡充值订单 3 - 销售充值订单
      pageSize: that.data.pageSize,
      pageIndex: that.data.pageIndex,
      keyword: that.data.searchValue
    }, function (res) {
      if (res.data.code == 0) {
        if (res.data.totalcount > 0) {
          that.data.pageIndex = that.data.pageIndex + 1
          console.log("订单列表数据", res)
          var orders = res.data.data.orderList;
          if(orders!=undefined && orders.length > 0){
            for (var index in orders) {
              that.data.orderList.splice(that.data.orderList.length, 0, orders[index]);
            }
            that.setData({
              hasData: true,
              orderList: that.data.orderList
            })
          }
        }
      }
      wx.stopPullDownRefresh() //停止下拉刷新
      that.setData({
         isLoading: false, loading: true, netWork: true,
      })
    }, function (res) {
        that.setData({ hasData: false,  isLoading: false,  loading: true, netWork: true })
    })
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新
  },
  onChange(e){
    console.log('onChange',e)
    this.setData({
      searchValue: e.detail
    });
  },
  onSearch(val){
    console.log('onSearch',val)
    this.data.pageIndex = 1;
    this.data.searchValue = val.detail;
    this.setData({
      orderList:[]
    })
    this.onHttpInit()
  },
  onSearchClick(val){
    console.log('onSearchClick',val)
    this.data.pageIndex = 1;
    this.setData({
      orderList:[]
    })
    this.onHttpInit()
  },
  goScanRecharge(res){
    console.log('scanCode res',res)
    var that = this;
    var isLogin = app.getStorageSync("isLogin");
    that.data.from_page = 'scancode'
    if (!isLogin) {
      wx.navigateTo({
        url: '/pages/login/index',
      })
    }else {
      wx.scanCode({
        success: (res) => {
          console.log("扫码结果", res);
          var qrcontent = res.result;
          console.log('扫码结果',qrcontent)
          var q = "?" + qrcontent.split("?")[1];

          // var json_param = commonJs.getQueryVariable(q);
          // var linkParam = "qrtype=device&id=" + json_param.id;
          // console.log('linkParam',linkParam)
          if(qrcontent.indexOf('https://') >= 0){
            var q = "?" + qrcontent.split("?")[1];
            var json_param = commonJs.getQueryVariable(q);
            if(qrcontent.indexOf('water/card/n') >= 0){//扫描水卡绑定二维码
              wx.navigateTo({
                url: '/pages/water/recharge/sale/index?card_num=' + json_param.n,
              })
            }  else {
              Toast.fail("当前二维码无效");
            }
          }
        },
        fail: (res) => { console.log(res); }
      })
    }
  },
  goOrderDetail:function(t){
    console.log('goOrderDetail t',t)
    var state = t.currentTarget.dataset.state
    var orderid = t.currentTarget.dataset.orderid;
    var orderType = t.currentTarget.dataset.ordertype;
    console.log('orderType',orderType);
    wx.navigateTo({
      url: "/pages/order/detail/index?state=" + state + "&order_id=" + orderid
    });
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.initOp()
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.setData({
      isLoading: true
    })
    this.onHttpInit()
  },
  cancelOrder:function(e){
    var order_id = e.currentTarget.dataset.orderid
    var that = this
    Dialog.confirm({
      title: '取消订单',
      message
    }).then(() => {
      that.onHttpCancelOrder(order_id)
    })
    .catch(() => {
      Dialog.close();
    });
  },
  onHttpCancelOrder: function (order_id){
    console.log('order_id',order_id)
    var that = this
    fetchApi.httpPost('/product/order/cancelOrder', {
      order_id: order_id,
    }, function (res) {
      console.log("取消订单 返回数据", res)
      if (res.data.code == 0) {
        Toast.success('取消成功');
        that.initOp()
      } else {
        Toast.fail(res.data.msg);
      }
    },function(){})
  },

})